% The second param is function with signature:
% get_probability_function(<current_simbol>, <processed_sequence_length>, <current_alphabet_size>, <alphabet_size>)

function [bits_count, probabilities] = adaptive_cost(x, alphabet_size, get_probability_function)
	if (isempty(x))
		bits_count = 0;
        probabilities = [];
		return;
	end;
	
 	l = numel(x);
	num = zeros(1, alphabet_size);
	probabilities = zeros(1, l);	
	current_alphabet_size = 0;
 	
	for i = 1:l
		idx = x(i) + 1;
		if (idx > alphabet_size)
			idx = alphabet_size
            disp('error');
		end;
		probabilities(i) = get_probability_function(num(idx), i - 1, current_alphabet_size, alphabet_size);
		if (num(idx) == 0)
			current_alphabet_size = current_alphabet_size + 1;
		end;
		num(idx) = num(idx) + 1;
	end;

	bits_count = ceil(1 - sum(log2(probabilities + (probabilities == 0))));
end
